package service

import (
	"ksd-social-api/modules/course/mapper"
	"ksd-social-api/modules/course/model"
	"ksd-social-api/modules/course/model/context"
	"ksd-social-api/modules/course/model/vo"
)

type CourseCommentService struct{}

var courseCommentMapper = mapper.CourseCommentMapper{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询课程信息的业务蹭
 */
func (service *CourseCommentService) SaveCourseComment(courseContext context.CourseCommentContext) (c *vo.CourseCommentVo, err error) {
	// 1： 准备课程评论模型
	comment := model.CourseComment{}
	// 2: 把入参上下文的数据全部倒入数据模型comment中
	comment.UserId = courseContext.UserId
	comment.UserAvatar = courseContext.UserAvatar
	comment.Username = courseContext.Username
	comment.CourseTitle = courseContext.CourseTitle
	comment.CourseId = courseContext.CourseId
	comment.Content = courseContext.Content
	comment.ParentId = courseContext.ParentId
	//执行数据模型入库
	dbComment, err := courseCommentMapper.Save(comment)
	if err != nil {
		return nil, err
	}
	// 3: 建设一个课程评论vo
	courseCommentVo := vo.CourseCommentVo{}
	courseCommentVo.UserId = dbComment.UserId
	courseCommentVo.UserAvatar = dbComment.UserAvatar
	courseCommentVo.Username = dbComment.Username
	courseCommentVo.CourseTitle = dbComment.CourseTitle
	courseCommentVo.CourseId = dbComment.CourseId
	courseCommentVo.Content = dbComment.Content
	courseCommentVo.ParentId = dbComment.ParentId
	courseCommentVo.ID = dbComment.ID
	courseCommentVo.CreateTime = dbComment.CreateTime
	// 返回课程的vo
	return &courseCommentVo, nil
}
